package com.hdu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdu.entity.School;
import com.hdu.entity.Weather;
import com.hdu.mapper.SchoolMapper;
import com.hdu.mapper.WeatherMapper;
import com.hdu.service.IWeatherService;
import com.hdu.vo.RegionSelect;
import com.hdu.vo.SchoolSelect;
import com.hdu.vo.WeatherTable;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 天气实体类 服务实现类
 * </p>
 *
 * @author xiaobai
 * @since 2025-02-07
 */
@Service
@RestController
@RequiredArgsConstructor
public class WeatherServiceImpl extends ServiceImpl<WeatherMapper, Weather> implements IWeatherService {

    private final WeatherMapper weatherMapper;

    private final SchoolMapper schoolMapper;

    @Override
    public List<Map<String, Object>> avg(String name, String time) {
        QueryWrapper<Weather> wrapper = new QueryWrapper<>();
        String column = "round(avg(" + name + "),2) as value";
        wrapper.select("region as name", column)
                .eq(StrUtil.isNotEmpty(time), "left(time,2)", time)
                .groupBy("region");
        return weatherMapper.selectMaps(wrapper);
    }

    @Override
    public List<RegionSelect> region() {
        List<RegionSelect> regionSelectList = new ArrayList<>();
        QueryWrapper<Weather> wrapper = new QueryWrapper<>();
        wrapper.select("region").groupBy("region");
        List<Object> region = weatherMapper.selectObjs(wrapper);
        for (Object object : region) {
            QueryWrapper<Weather> query = new QueryWrapper<>();
            query.select("city").eq("region", object).groupBy("city");
            regionSelectList.add(new RegionSelect(object, weatherMapper.selectObjs(query)));
        }
        return regionSelectList;
    }

    @Override
    public WeatherTable weather(String name) {
        WeatherTable weatherTable = new WeatherTable();
        weatherTable.setAvgMax(weatherList("round(avg(max_temp), 2)", name));
        weatherTable.setAvgMin(weatherList("round(avg(min_temp), 2)", name));
        weatherTable.setIndices(weatherList("round(avg(indices), 2)", name));
        weatherTable.setMinTemp(weatherList("min(min_temp)", name));
        weatherTable.setMaxTemp(weatherList("max(max_temp)", name));
        weatherTable.setWeather(weatherRate("round(count(*) / 3.65, 2)", name, "weather", new String[]{"晴", "阴", "雨"}));
        weatherTable.setQuality(weatherRate("round(count(*) / 3.65, 2)", name, "quality", new String[]{"优", "良", "污染"}));
        return weatherTable;
    }

    @Override
    public List<SchoolSelect> school() {
        QueryWrapper<School> wrapper = new QueryWrapper<>();
        wrapper.select("name", "province", "city");
        return BeanUtil.copyToList(schoolMapper.selectList(wrapper), SchoolSelect.class);
    }

    public List<Object> weatherRate(String sql, String name, String column, String[] values) {
        List<Object> list = new ArrayList<>();
        for (String s : values) {
            list.add(weatherMapper.selectObjs(new QueryWrapper<Weather>().select(sql).eq("city", name).like(column, s)).get(0));
        }
        return list;
    }

    public List<Object> weatherList(String sql, String name) {
        QueryWrapper<Weather> wrapper = new QueryWrapper<>();
        wrapper.select(sql).eq("city", name).groupBy("LEFT(time, 2)");
        return weatherMapper.selectObjs(wrapper);
    }

}
